using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for SessionsControl.
	/// </summary>
	public class SessionsControl : System.Windows.Forms.UserControl
	{
    private ExtendedDataGrid sessionsGrid;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private IContainer components;

		public SessionsControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Refresh", "Refresh the list of sessions", 
        new ActionExecuteDelegate(RefreshSessions));
      actionList.Add("Copy to clipboard", "Copy the list of sessions to the clipboard", 
        new ActionExecuteDelegate(CopyToClipboard));
      actionList.Add("Delete selected", "Delete the selected sessions", 
        new ActionExecuteDelegate(DeleteSelected));
      actionList.Add("Delete expired", "Delete users sessions with no activity for more than a day (engine sessions will not be removed)", 
        new ActionExecuteDelegate(DeleteExpired));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.sessionsGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.sessionsGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // sessionsGrid
      // 
      this.sessionsGrid.AllowNavigation = false;
      this.sessionsGrid.CaptionVisible = false;
      this.sessionsGrid.DataMember = "";
      this.sessionsGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.sessionsGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.sessionsGrid.Location = new System.Drawing.Point(0, 0);
      this.sessionsGrid.Name = "sessionsGrid";
      this.sessionsGrid.Size = new System.Drawing.Size(594, 392);
      this.sessionsGrid.TabIndex = 0;
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(594, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 392);
      this.uiActionsControl.TabIndex = 2;
      // 
      // SessionsControl
      // 
      this.Controls.Add(this.sessionsGrid);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "SessionsControl";
      this.Size = new System.Drawing.Size(744, 392);
      ((System.ComponentModel.ISupportInitialize)(this.sessionsGrid)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    private SessionCollection sessions;
    public void LoadSessions(SessionCollection sessions)
    {
      this.sessions = sessions;
      sessionsGrid.DataSource = sessions;
      sessionsGrid.ClearColumns();
      sessionsGrid.AddColumn("UserName", "User");
      sessionsGrid.AddColumn("SessionId", "Session ID");
      sessionsGrid.AddColumn("CallerId", "Caller ID");
      sessionsGrid.AddColumn("LastActivity", "Last activity");
      sessionsGrid.AddColumn("Timeout");
      sessionsGrid.AutosizeColumns();
    }

    private string DeleteSelected()
    {
      using (new CursorHandler())
      {
        sessionsGrid.DeleteSelected();
      }
      return null;
    }

    private string DeleteExpired()
    {
      int deleted = sessions.DeleteExpired();
      sessionsGrid.RefreshGrid();
      return deleted + " sessions deleted";
    }

    private string RefreshSessions()
    {
      sessions.Refresh();
      sessionsGrid.RefreshGrid();
      return null;
    }

    private string CopyToClipboard()
    {
      sessionsGrid.CopyToClipboard();
      return null;
    }
	}
}
